package cn.lrwjz.androidguide.ui.exmple;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.google.android.material.appbar.MaterialToolbar;

import cn.lrwjz.androidguide.R;
import cn.lrwjz.androidguide.model.User;

public class IntentTwoActivity extends AppCompatActivity {

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_intent_two);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Intent 跳转页面示例");
        }

        // 获取页面传递参数
        Intent intent = getIntent();
        if (intent != null) {
            // 获取单参数
            String name = intent.getStringExtra("name");
            TextView tvName = findViewById(R.id.tv_name);
            tvName.setText(name);

            // 获取多参数
            Bundle bundle = intent.getExtras();
            int age = bundle.getInt("age");
            String sex = bundle.getString("sex");
            TextView tvAge = findViewById(R.id.tv_age);
            tvAge.setText(String.valueOf(age));
            TextView tvSex = findViewById(R.id.tv_sex);
            tvSex.setText(sex);

            // 获取对象参数
            User user = (User) intent.getSerializableExtra("user");
            assert user != null;
            Log.d("IntentTwoActivity", "onCreate: " + user);

        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            // 回传数据
            Intent intent = new Intent();
            intent.putExtra("result", "回传的数据");
            setResult(RESULT_OK, intent);

            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}